package org.huiway.huipicturebackend.utils.mail;
 
 
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.time.Duration;
 
/**
 * @description: 发送邮箱业务
 */
@Component
public class MailMsg {

    @Resource
    private JavaMailSenderImpl mailSender;
    @Resource
    private RedisTemplate<String,String> redisTemplate;

    public boolean mail(String email) throws MessagingException {

        MimeMessage mimeMessage = mailSender.createMimeMessage();
        //生成随机6位验证码
        String code = CodeGeneratorUtil.generateCode(6);
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        //设置一个html邮件信息
        String content = "<div style=\"max-width: 500px; margin: 0 auto; font-family: Arial, sans-serif; border: 1px solid #e5e5e5; border-radius: 8px; padding: 20px; box-shadow: 0 4px 8px rgba(0,0,0,0.05); background-color: #ffffff;\">" +
                "<h2 style=\"color: #1677ff; text-align: center;\">慧云图库邮箱验证</h2>" +
                "<p style=\"font-size: 15px; color: #333;\">您好，</p>" +
                "<p style=\"font-size: 15px; color: #333;\">您正在进行邮箱验证操作，请在页面中输入以下验证码完成验证：</p>" +
                "<div style=\"text-align: center; margin: 24px 0;\">" +
                "<span style=\"display: inline-block; font-size: 28px; letter-spacing: 8px; padding: 10px 20px; background-color: #f5f5f5; border-radius: 6px; color: #1677ff; font-weight: bold;\">" +
                code +
                "</span>" +
                "</div>" +
                "<p style=\"font-size: 14px; color: #999;\">验证码有效期为 <strong>1 分钟</strong>，请及时使用。</p>" +
                "<hr style=\"border: none; border-top: 1px solid #eee; margin: 24px 0;\" />" +
                "<p style=\"font-size: 12px; color: #aaa; text-align: center;\">本邮件由系统自动发送，请勿直接回复。</p>" +
                "</div>";
        helper.setText(content, true);
        //设置邮件主题名
        helper.setSubject("慧云图库验证码----验证码");
        //发给谁-》邮箱地址
        helper.setTo(email);
        //谁发的-》发送人邮箱
        //设置发送者名称
        helper.setFrom("慧云 <2797897359@qq.com>");
        //将邮箱验证码以邮件地址为key存入redis,3分钟过期
        redisTemplate.opsForValue().set(email, code, Duration.ofMinutes(1));
        mailSender.send(mimeMessage);
        return true;
    }
}